package com.zy.design.pattern.di.meat;

import cn.hutool.core.util.XmlUtil;
import com.google.common.collect.Maps;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.Map;

/**
 * @author zhaoyi
 * @date 2021/4/1 11:26
 */
public class MeatParse {


    public static Map<String, ClassMeat> parse() throws ClassNotFoundException {
        Document document = XmlUtil.readXML("beans.xml");
        NodeList list = document.getElementsByTagName("bean");
        Map<String, ClassMeat> map = Maps.newHashMapWithExpectedSize(list.getLength());
        for (int i = 0; i < list.getLength(); i++) {
            Element element = (Element) list.item(i);
            ClassMeat classMeat = new ClassMeat(element);
            map.put(classMeat.getId(), classMeat);
        }
        for (ClassMeat value : map.values()) {
            value.getConstructorArgsList().stream()
                    .filter(ConstructorArgs::isHasRef)
                    .forEach(f -> {
                        String id = (String) f.getValue();
                        ClassMeat classMeat = map.get(id);
                        f.setType(classMeat.getClazzName());
                    });
        }
        return map;
    }
}
